package com.example.ssm.service;

import com.example.ssm.dao.UserDao;
import com.example.ssm.pojo.user.login.MessageModel_Login;
import  com.example.ssm.pojo.user.register.MessageModel_registered;
import com.example.ssm.pojo.User;
import com.example.ssm.pojo.user.register.MessageModel_registered;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Package: com.example.ssm.service
 * @ClassName: UserService
 * @Author: 76470
 * @CreateTime: 2021/6/8 10:39
 * @Description:
 */
@Service
public class UserService {
	@Autowired
	private UserDao userDao;
	
	/*登录服务*/
	public MessageModel_Login UserLoginService (String userName, String passWord){
		MessageModel_Login messageModel_login = new MessageModel_Login ();
		System.out.println ("Service接收到：#"+userName+"#  "+"#"+passWord+"#");//test
		System.out.println ("正确密码："+userDao.findPwdByName ( userName ));//test
		//如果密码不正确
		if(!passWord.equals ( userDao.findPwdByName ( userName ) )){
			messageModel_login.setCode ( false );
			messageModel_login.setToken ( null );
			System.out.println ("返回结果:"+"false  "+messageModel_login);//test
			return messageModel_login;
		}
		else{
			messageModel_login.setCode ( true );
			messageModel_login.setToken ( userDao.findTokenByName ( userName ) );
			System.out.println ("返回结果:"+"true  "+messageModel_login);//test
			return messageModel_login;
		}
	}
	
	/*获取用户信息服务*/
	public User UserGetInfoService(Integer token){
		System.out.println ("Service接收到的token:"+token);//test
		System.out.println ("返回的信息:"+userDao.findAllByToken ( token ));//test
		return userDao.findAllByToken ( token );
	}
	
	/*注册服务*/
	public MessageModel_registered UserRegisteredService ( String userName, String passWord, String introduction ){
		MessageModel_registered messageModel_registered = new MessageModel_registered ();
		System.out.println ("Service接收到的数据:"+userName+","+passWord+","+introduction);//test
		//如果数据库表中已存在同名账户
		if(userDao.findAllByName ( userName ) != null){
			messageModel_registered.setCode ( false );
			System.out.println ("返回的信息:"+messageModel_registered);//test
			return messageModel_registered;
		}
		else{
			//向数据库表中增加数据
			userDao.addCount ( userName, passWord, introduction );
			messageModel_registered.setCode ( true );
			System.out.println ("返回的信息:"+messageModel_registered);//test
			return messageModel_registered;
		}
	}
	
	
}
